package gold.digger;

import gold.utils.InputUtil;
import javafx.util.Pair;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.TreeMap;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1405 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String longestDiverseString(int a, int b, int c) {
            PriorityQueue<int[]> q = new PriorityQueue<>((x, y) -> y[1] - x[1]);
            if (a > 0) q.add(new int[]{0, a});
            if (b > 0) q.add(new int[]{1, b});
            if (c > 0) q.add(new int[]{2, c});
            StringBuilder sb = new StringBuilder();
            while (!q.isEmpty()) {
                int[] cur = q.poll();
                int n = sb.length();
                if (n >= 2 && sb.charAt(n - 1) - 'a' == cur[0] && sb.charAt(n - 2) - 'a' == cur[0]) {
                    if (q.isEmpty()) break;
                    int[] next = q.poll();
                    sb.append((char) (next[0] + 'a'));
                    if (--next[1] != 0) q.add(next);
                    q.add(cur);
                } else {
                    sb.append((char) (cur[0] + 'a'));
                    if (--cur[1] != 0) q.add(cur);
                }
            }
            return sb.toString();
        }
    }


    class Solution_Wrong_Thought {
        public String longestDiverseString(int a, int b, int c) {
            PriorityQueue<Pair<Character, Integer>> q = new PriorityQueue<>(new Comparator<Pair<Character, Integer>>() {
                @Override
                public int compare(Pair<Character, Integer> o1, Pair<Character, Integer> o2) {
                    return o2.getValue() - o1.getValue();
                }
            });

            q.add(new Pair<>('a', a));
            q.add(new Pair<>('b', b));
            q.add(new Pair<>('c', c));

            StringBuilder ans = new StringBuilder();
            while (!q.isEmpty()) {
                Pair<Character, Integer> curMax = q.poll(), curMid = q.poll(), curMin = q.poll();
                if (curMax.getValue() >= 2 && (curMin.getValue() >= 1 || curMid.getValue() >= 1)) {
                    if (curMin.getValue() < 1) {
                        Pair<Character, Integer> temp = curMin;
                        curMin = curMid;
                        curMid = temp;
                    }

                    ans.append(curMax.getKey()).append(curMax.getKey()).append(curMin.getKey());
                    q.add(new Pair<>(curMax.getKey(), curMax.getValue() - 2));
                    q.add(new Pair<>(curMin.getKey(), curMin.getValue() - 1));
                    q.add(curMid);
                } else {
                    int cnt = Math.min(2, curMax.getValue());
                    for (int i = 0; i < cnt; i++) {
                        ans.append(curMax.getKey());
                    }
                    break;
                }
            }

            return ans.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();

//        示例 1：
//
//        输入：a = 1, b = 1, c = 7
//        输出："ccaccbcc"
//        解释："ccbccacc" 也是一种正确答案。
        System.out.println(solution.longestDiverseString(1, 1, 7));

//        示例 2：
//
//        输入：a = 2, b = 2, c = 1
//        输出："aabbc"
        System.out.println(solution.longestDiverseString(2, 2, 1));
        System.out.println(solution.longestDiverseString(7, 1, 0));


        System.out.println(solution.longestDiverseString(1, 3, 5));

    }

    public static void main(String[] args) throws Exception {
        LC1405 an = new LC1405();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
